package com.ido.otaclient.view;

import android.view.View;
import android.widget.RadioGroup;

import androidx.viewpager.widget.ViewPager;

/**
 * Copyright (c) 2018 桂林智神信息技术有限公司. All rights reserved.
 *
 * @author cnjer
 * @date 2018/3/28
 * @description
 */

public class SetUpUtil {

    /**
     * 结合viewPager和radioGroup
     *
     * @param radioGroup
     * @param viewPager
     */
    public static void setUpWithRadioGroupAndViewPager(RadioGroup radioGroup, ViewPager viewPager) {
        radioGroup.setOnCheckedChangeListener((group, checkedId) -> {
            int checkPosition = group.indexOfChild(group.findViewById(checkedId));
            viewPager.setCurrentItem(checkPosition);
        });
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                View view = radioGroup.getChildAt(position);
                if (view != null) {
                    radioGroup.check(view.getId());
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }
}
